using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace OpenRefineEngine.Operations
{
    public class EngineConfig
    {

        [JsonConverter(typeof(FacetsConverter))]
        public List<Facet.Facet> Facets { get; set; }
        public string Mode { get; set; }
    }

    /// <summary>
    /// When has no facets property appears as an empty array instead of null or absent property in JSON file.
    /// With this converter we see if is a list of Facets otherwise will be an empty list.
    /// </summary>
    public class FacetsConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(List<Facet.Facet>));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var value = serializer.Deserialize(reader);
            return value is List<Facet.Facet> ? value : new List<Facet.Facet>();
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value);
        }
    }
}